package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.RoadLineDetailsActivity;
import com.runen.infinitemovement.model.entity.PathSectionListEntity;
import com.runen.infinitemovement.model.enums.ChallengeType;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

public class AllRoadAdapter extends BaseQuickAdapter<PathSectionListEntity, BaseViewHolder> {
    private Context context;
    private int position = 0;

    private OnClick onClick;

    public void checkPosition(int position) {
        this.position = position;
    }

    public AllRoadAdapter(Context context, OnClick onClick) {
        super(R.layout.item_all_road);
        this.context = context;
        this.onClick = onClick;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, PathSectionListEntity item) {
        LinearLayout ll_all_road_bg = helper.getView(R.id.ll_all_road_bg);
        TextView tv_last_and_next_name = helper.getView(R.id.tv_last_and_next_name);
        TextView tv_jp_title = helper.getView(R.id.tv_jp_title);
        TextView tv_jangpin = helper.getView(R.id.tv_jangpin);
        TextView tv_see_details = helper.getView(R.id.tv_see_details);
        TextView tv_challenge = helper.getView(R.id.tv_challenge);
        ImageView iv_challenge = helper.getView(R.id.iv_challenge);
        ImageView iv_finish = helper.getView(R.id.iv_finish);
        RoundedCornersImage rci_bg = helper.getView(R.id.rci_bg);
//        tv_jp_title.setVisibility(View.GONE);
//        tv_jangpin.setVisibility(View.GONE);
        if (item.bannerType == 1) {
            GlideUtils.loadImage(context, item.linkAddr + "?x-oss-process=video/snapshot,t_0", rci_bg);
        } else {
            GlideUtils.loadImage(context, item.linkAddr, rci_bg);
        }
        String roadName = "%s - %s";
        tv_last_and_next_name.setText(String.format(roadName, item.startPoint, item.endPoint));
        tv_jangpin.setText(TextUtils.isEmpty(item.prize) ? "暂无奖品" : item.prize);
        switch (item.challenge) {
            case ChallengeType.UNFINISH:
                iv_challenge.setVisibility(View.GONE);
                iv_finish.setVisibility(View.GONE);
                break;
            case ChallengeType.CHALLENGE:
                iv_challenge.setVisibility(View.VISIBLE);
                iv_finish.setVisibility(View.GONE);
                break;
            case ChallengeType.FINISH:
                iv_challenge.setVisibility(View.GONE);
                iv_finish.setVisibility(View.VISIBLE);
                break;
        }
        if (helper.getAdapterPosition() == position) {
            tv_last_and_next_name.setTextColor(CommonUtil.getColor(R.color.white));
            tv_jp_title.setTextColor(CommonUtil.getColor(R.color.white));
            tv_jangpin.setTextColor(CommonUtil.getColor(R.color.white));
            tv_see_details.setTextColor(CommonUtil.getColor(R.color.white));
            tv_see_details.setBackground(CommonUtil.getDrawable(R.drawable.bg_white_stroke_360dp));
            tv_challenge.setTextColor(CommonUtil.getColor(R.color.white));
            tv_challenge.setBackground(CommonUtil.getDrawable(R.drawable.bg_white_stroke_360dp));
            ll_all_road_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_ff9949fc2a42_dp8));
        } else {
            tv_last_and_next_name.setTextColor(CommonUtil.getColor(R.color.color_333333));
            tv_jp_title.setTextColor(CommonUtil.getColor(R.color.color_FF999999));
            tv_jangpin.setTextColor(CommonUtil.getColor(R.color.color_FF666666));
            tv_see_details.setTextColor(CommonUtil.getColor(R.color.color_FFFD2E43));
            tv_see_details.setBackground(CommonUtil.getDrawable(R.drawable.bg_ed3f37_dp360));
            tv_challenge.setTextColor(CommonUtil.getColor(R.color.color_FFFD2E43));
            tv_challenge.setBackground(CommonUtil.getDrawable(R.drawable.bg_ed3f37_dp360));
            ll_all_road_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_white_cr_dp8));
        }
        tv_see_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RoadLineDetailsActivity.class);
                intent.putExtra("pathId", item.sectionId);
                context.startActivity(intent);
            }
        });
        tv_challenge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.challenge(item.sectionId);
                }
            }
        });
    }

    public interface OnClick {
        void challenge(String sectionId);
    }
}
